from sqlalchemy import Column, Integer, String, DateTime, Text, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from database import Base

from model.cdn_config_node import cdn_config_node


class CdnConfig(Base):
    __tablename__ = "t_cdn_config"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    domain = Column(String)
    originIp = Column(String)
    ifDeploy = Column(Boolean) ## 是否部署
    fullchain = Column(Text, nullable=True)
    privkey = Column(Text, nullable=True)
    cert_apply_time = Column(DateTime, nullable=True)  # 证书申请时间

    # 关联单个 cname
    cname_id = Column(Integer, ForeignKey("t_cname.id"), unique=False, nullable=False)
    cname = relationship("CName", backref="cdn_config")

    # 多对多关联节点
    nodes = relationship(
        "CdnNode",
        secondary=cdn_config_node,
        back_populates="configs"
    )
